Linq, join, group, count > select more values?

From s in context. Suppliers join p in context. Products on s equals p.

Supplier group s by s. CompanyName into result select new { SupplierName = result. Key, ProductCount = result.Count(), SupplierId = result.First().

Id, SuppliedAddress = result.First(). Address } It would look more natural if you grouped by Id instead, or maybe all of them: from s in context. Suppliers join p in context.

Products on s equals p. Supplier group s by new { s. CompanyName, s.

Id, s. Address } into result select new { ProductCount = result.Count(), SupplierName = result.Key. CompanyName, SupplierId = result.Key.

Id, SuppliedAddress = result.Key. Address }.

3 Implicit in result.FirstOrDefault(). Id and result.FirstOrDefault(). Address is the possibility of a null exception.

– spender Aug 9 at 23:28 1 I concur with splender. If you are going to use the value without checking for null, there's no point in using the OrDefault versions. – svick Aug 9 at 23:34 @svick: You're absolutely right, for some reason I imagined there is no First() method in LINQ.

This is getting really late here and I think I need to get some sleep.. – Dan Abramov Aug 9 at 23:36 Yes, you're completely right that it's better to group by Id, but I just typed this as a quick example to make my question clear. – TysHTTP Aug 9 at 23:36 The second example suits perfect! Thanks!

– TysHTTP Aug 9 at 23:38.

EDIT Um... Unless I'm mistaken, this can be done considerably more cleanly: context . Products . GroupBy(p=>p.

Supplier) . Select(result=>new { SupplierName = result. Key, ProductCount = result.Count(), SupplierId = result.Key.Id, SupplierAddress = result.Key.

Address, } The joins come out of the box from the FK relationships in the DB, so a Product already has a Supplier. It seems you spotted this setup in your own code ( ...equals p. Supplier ), then failed to understand its meaning.

Apologies for changing from comprehension syntax to method chains. They come more naturally to me. Supplemental to @Dan's comment (which is likely correct for Linq2Objects), in Linq2Sql (I can't vouch for L2E, but I imagine it's much the same), if you group by a property that is generated by a FK relationship, the resulting generated SQL will GROUP BY the key value, not the entire entity.

1 Note that you'll also need Supplier to implement IEquatable for this to work. – Dan Abramov Aug 9 at 23:37 @Dan, I extended my answer to address your comment. – spender Aug 9 at 23:54 You're totally right, I overlooked that it's LINQ to SQL.

– Dan Abramov Aug 10 at 0:10.

Join p in context. Products on s equals p. Group s by s.

SupplierName = result. SupplierId = result.First().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions